/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelEspacoDisponivel.java
 *
 * Created on 19/09/2011, 06:20:10
 */

package modules.enderecamento.gui.jpanel;

import java.text.DecimalFormat;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import modules.enderecamento.bo.RelatorioBO;
import modules.enderecamento.facade.WMSFacade;
import modules.enderecamento.gui.custom.LimitedIntegerJTextField;
import modules.enderecamento.gui.custom.WMSJTextField;
import modules.enderecamento.models.dto.Palete;
import modules.enderecamento.models.dto.Rua;
import modules.enderecamento.models.dto.custom.OcupacaoPalete;
import modules.enderecamento.models.dto.custom.OcupacaoRua;
import modules.enderecamento.util.BoxPanel;

/**
 *
 * @author Clezio
 */
public class PanelEspacoFisico extends javax.swing.JPanel {

    private WMSFacade wmsFacade;
    private Integer percentualDisponivel = null;
    private Integer percentualOcupado = null;
    private Rua ruaGlobal;
    private Palete paleteGlobal;
    /** Creates new form PanelEspacoDisponivel */
    public PanelEspacoFisico() {
        initComponents();
        wmsFacade = new WMSFacade();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        tfRua = new modules.enderecamento.gui.custom.WMSJTextField();
        jLabel1 = new javax.swing.JLabel();
        wMSLabel1 = new modules.enderecamento.gui.custom.WMSLabel();
        wMSLabel2 = new modules.enderecamento.gui.custom.WMSLabel();
        tfPalete = new LimitedIntegerJTextField(10);
        btnAdicionar = new modules.enderecamento.gui.custom.WMSJButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableOcupacaoRuas = new modules.enderecamento.gui.custom.WMSJTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableOcupacaoPaletes = new modules.enderecamento.gui.custom.WMSJTable();
        btnImprimirRua = new modules.enderecamento.gui.custom.WMSJButton();
        btnImprimirPalete = new modules.enderecamento.gui.custom.WMSJButton();
        rbTodos = new modules.enderecamento.gui.custom.WMSJRadioButton();
        rbOcupado = new modules.enderecamento.gui.custom.WMSJRadioButton();
        rbDisponivel = new modules.enderecamento.gui.custom.WMSJRadioButton();
        wMSLabel3 = new modules.enderecamento.gui.custom.WMSLabel();
        wMSLabel4 = new modules.enderecamento.gui.custom.WMSLabel();
        btnLimparFiltro = new modules.enderecamento.gui.custom.WMSJButton();

        setBackground(new java.awt.Color(238, 238, 238));
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        tfRua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfRuaActionPerformed(evt);
            }
        });
        tfRua.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfRuaFocusLost(evt);
            }
        });
        tfRua.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfRuaKeyPressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Consulta espaço físico");

        wMSLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        wMSLabel1.setText("Rua:");

        wMSLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        wMSLabel2.setText("Palete:");

        tfPalete.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfPaleteFocusLost(evt);
            }
        });
        tfPalete.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfPaleteKeyPressed(evt);
            }
        });

        btnAdicionar.setText("Filtrar");
        btnAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarActionPerformed(evt);
            }
        });

        jTableOcupacaoRuas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Rua", "Ocupação (Palete)", "Disponível (Palete)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableOcupacaoRuas);

        jTableOcupacaoPaletes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "Palete", "Rua", "Ocupação (m³)", "Disponível (m³)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTableOcupacaoPaletes);

        btnImprimirRua.setText("Imprimir");
        btnImprimirRua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirRuaActionPerformed(evt);
            }
        });

        btnImprimirPalete.setText("Imprimir");
        btnImprimirPalete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirPaleteActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbTodos);
        rbTodos.setSelected(true);
        rbTodos.setText("Todos");
        rbTodos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                rbTodosMouseClicked(evt);
            }
        });
        rbTodos.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                rbTodosStateChanged(evt);
            }
        });
        rbTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbTodosActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbOcupado);
        rbOcupado.setText("Ocupado");
        rbOcupado.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                rbOcupadoStateChanged(evt);
            }
        });
        rbOcupado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbOcupadoActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbDisponivel);
        rbDisponivel.setText("Disponivel");
        rbDisponivel.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                rbDisponivelStateChanged(evt);
            }
        });
        rbDisponivel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbDisponivelActionPerformed(evt);
            }
        });

        wMSLabel3.setBackground(new java.awt.Color(255, 255, 204));
        wMSLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        wMSLabel3.setText("Ruas");
        wMSLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));

        wMSLabel4.setBackground(new java.awt.Color(255, 255, 204));
        wMSLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        wMSLabel4.setText("Paletes");
        wMSLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));

        btnLimparFiltro.setText("Limpar Filtro");
        btnLimparFiltro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparFiltroActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 780, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(wMSLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfRua, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(wMSLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfPalete, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(rbOcupado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(rbTodos, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                            .addComponent(rbDisponivel, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLimparFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(10, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(147, 147, 147)
                        .addComponent(btnImprimirRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 327, Short.MAX_VALUE)
                        .addComponent(btnImprimirPalete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(148, 148, 148))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(wMSLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(wMSLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE))))
                .addGap(26, 26, 26))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addComponent(wMSLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addComponent(wMSLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(tfPalete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rbTodos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbOcupado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rbDisponivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnLimparFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(wMSLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(wMSLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnImprimirRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnImprimirPalete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tfRuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfRuaActionPerformed

}//GEN-LAST:event_tfRuaActionPerformed

    private void tfRuaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfRuaFocusLost
        
}//GEN-LAST:event_tfRuaFocusLost

    private void tfRuaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfRuaKeyPressed
        
}//GEN-LAST:event_tfRuaKeyPressed

    private void tfPaleteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfPaleteFocusLost
       
}//GEN-LAST:event_tfPaleteFocusLost

    private void tfPaleteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfPaleteKeyPressed
        
}//GEN-LAST:event_tfPaleteKeyPressed

    private void btnAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarActionPerformed
        filtrarRegistros();
}//GEN-LAST:event_btnAdicionarActionPerformed

    private void btnImprimirRuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirRuaActionPerformed
        new RelatorioBO().espacoFisicoRua(ruaGlobal, percentualDisponivel, percentualOcupado);
    }//GEN-LAST:event_btnImprimirRuaActionPerformed

    private void btnImprimirPaleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirPaleteActionPerformed
        new RelatorioBO().espacoFisicoPalete(ruaGlobal, paleteGlobal, percentualDisponivel, percentualOcupado);
    }//GEN-LAST:event_btnImprimirPaleteActionPerformed

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        listarOcupacaoRuas(wmsFacade.getListOcupacaoRuas(null,null,null));
        listarOcupacaoPaletes(wmsFacade.getListOcupacaoPaletes(null,null,null,null));
    }//GEN-LAST:event_formAncestorAdded

    private void rbTodosStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_rbTodosStateChanged
       
    }//GEN-LAST:event_rbTodosStateChanged

    private void rbOcupadoStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_rbOcupadoStateChanged
        
    }//GEN-LAST:event_rbOcupadoStateChanged

    private void rbDisponivelStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_rbDisponivelStateChanged
     
    }//GEN-LAST:event_rbDisponivelStateChanged

    private void rbTodosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rbTodosMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_rbTodosMouseClicked

    private void rbTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbTodosActionPerformed
        filtrarRegistros();
    }//GEN-LAST:event_rbTodosActionPerformed

    private void rbOcupadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbOcupadoActionPerformed
        filtrarRegistros();
    }//GEN-LAST:event_rbOcupadoActionPerformed

    private void rbDisponivelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbDisponivelActionPerformed
        filtrarRegistros();
    }//GEN-LAST:event_rbDisponivelActionPerformed

    private void btnLimparFiltroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparFiltroActionPerformed
        clearFields();
        filtrarRegistros();
    }//GEN-LAST:event_btnLimparFiltroActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private modules.enderecamento.gui.custom.WMSJButton btnAdicionar;
    private modules.enderecamento.gui.custom.WMSJButton btnImprimirPalete;
    private modules.enderecamento.gui.custom.WMSJButton btnImprimirRua;
    private modules.enderecamento.gui.custom.WMSJButton btnLimparFiltro;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private modules.enderecamento.gui.custom.WMSJTable jTableOcupacaoPaletes;
    private modules.enderecamento.gui.custom.WMSJTable jTableOcupacaoRuas;
    private modules.enderecamento.gui.custom.WMSJRadioButton rbDisponivel;
    private modules.enderecamento.gui.custom.WMSJRadioButton rbOcupado;
    private modules.enderecamento.gui.custom.WMSJRadioButton rbTodos;
    private modules.enderecamento.gui.custom.WMSJTextField tfPalete;
    private modules.enderecamento.gui.custom.WMSJTextField tfRua;
    private modules.enderecamento.gui.custom.WMSLabel wMSLabel1;
    private modules.enderecamento.gui.custom.WMSLabel wMSLabel2;
    private modules.enderecamento.gui.custom.WMSLabel wMSLabel3;
    private modules.enderecamento.gui.custom.WMSLabel wMSLabel4;
    // End of variables declaration//GEN-END:variables

    private void listarOcupacaoRuas(List<OcupacaoRua> ocupacaoRuas) {
        jTableOcupacaoRuas.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTableOcupacaoRuas.getColumnModel().getColumn(1).setPreferredWidth(50);
        jTableOcupacaoRuas.getColumnModel().getColumn(2).setPreferredWidth(50);

        DefaultTableModel modelo = (DefaultTableModel) jTableOcupacaoRuas.getModel();
        modelo.setNumRows(0);

        for (int i = 0; i < ocupacaoRuas.size(); i++) {
            String rua = ocupacaoRuas.get(i).getRua().getNome();
            Integer ocupacao = ocupacaoRuas.get(i).getOcupado();
            Integer disponivel = ocupacaoRuas.get(i).getDisponivel();
            DecimalFormat df = new DecimalFormat("##00");
            modelo.addRow(new Object[]{
                        rua, df.format(ocupacao), df.format(disponivel)
                    });
        }
    }

    private void listarOcupacaoPaletes(List<OcupacaoPalete> ocupacaoPaletes) {
        jTableOcupacaoPaletes.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTableOcupacaoPaletes.getColumnModel().getColumn(1).setPreferredWidth(50);
        jTableOcupacaoPaletes.getColumnModel().getColumn(2).setPreferredWidth(50);
        jTableOcupacaoPaletes.getColumnModel().getColumn(3).setPreferredWidth(50);

        DefaultTableModel modelo = (DefaultTableModel) jTableOcupacaoPaletes.getModel();
        modelo.setNumRows(0);

        for (int i = 0; i < ocupacaoPaletes.size(); i++) {
            String nameRua = "Nenhuma";
            if(ocupacaoPaletes.get(i).getRua() != null){
                nameRua = ocupacaoPaletes.get(i).getRua().getNome();
            }
            Integer paleteId = ocupacaoPaletes.get(i).getPalete().getId();
            Double ocupacao = ocupacaoPaletes.get(i).getOcupado();
            Double disponivel = ocupacaoPaletes.get(i).getDisponivel();
            DecimalFormat df = new DecimalFormat("##,###00.000");
            modelo.addRow(new Object[]{
                        paleteId,nameRua, df.format(ocupacao), df.format(disponivel)
                    });
        }
    }

    private void filtrarRegistros(){
        percentualDisponivel = null;
        percentualOcupado = null;
        ruaGlobal = null;
        paleteGlobal = null;
        if(rbDisponivel.isSelected()){
            try{
                String valorDigitado = BoxPanel.showInputDialog("Informe o percentual mínimo de disponibilidade:");
                if(valorDigitado.length() > 0){
                    percentualDisponivel = Integer.parseInt(valorDigitado);
                    if(percentualDisponivel > 100 || percentualDisponivel < 1){
                        throw new Exception("Percentual inválido");
                    }
                }
            }catch(Exception e){
                BoxPanel.showMessageDialog(null, "Percentual inválido");
                rbTodos.setSelected(true);
                filtrarRegistros();
            }
        }else if(rbOcupado.isSelected()){
            try{
                String valorDigitado = BoxPanel.showInputDialog(new WMSJTextField(),"Informe o percentual mínimo de ocupação:");
                if(valorDigitado.length() > 0){
                    percentualOcupado = Integer.parseInt(valorDigitado);
                    if(percentualOcupado > 100 || percentualOcupado < 1){
                        throw new Exception("Percentual inválido");
                    }
                }
            }catch(Exception e){
                BoxPanel.showMessageDialog(null, "Percentual inválido");
                rbTodos.setSelected(true);
                filtrarRegistros();
            }
        }

        Rua rua = null;
        Palete palete = null;

        if(tfRua.getText().length() > 0){
            rua = wmsFacade.obterRuaByName(tfRua.getText());
            if(rua == null){
                BoxPanel.showMessageDialog(btnAdicionar, "Rua não encontrada");
                tfRua.requestFocus();
                return;
            }
        }
        if(tfPalete.getText().length() > 0){
            palete = wmsFacade.obterPaleteById(Integer.parseInt(tfPalete.getText()));
            if(palete == null){
                BoxPanel.showMessageDialog(btnAdicionar, "Palete não encontrado");
                tfPalete.requestFocus();
                return;
            }
        }
        listarOcupacaoRuas(
                wmsFacade.getListOcupacaoRuas(rua, percentualDisponivel, percentualOcupado)
        );

        listarOcupacaoPaletes(
                wmsFacade.getListOcupacaoPaletes(rua,palete, percentualDisponivel, percentualOcupado)
        );

        
        if(percentualDisponivel != null){
            DecimalFormat df = new DecimalFormat("##00");
            rbDisponivel.setText("Disponível "+df.format(percentualDisponivel)+"%");
        }else{
            rbDisponivel.setText("Disponível");
        }

        if(percentualOcupado != null){
            DecimalFormat df = new DecimalFormat("##00");
            rbOcupado.setText("Ocupado "+df.format(percentualOcupado)+"%");
        }else{
            rbOcupado.setText("Ocupado");
        }
        ruaGlobal = rua;
        paleteGlobal = palete;
        //
    }

    private void clearFields() {
        tfRua.setText("");
        tfRua.setText("");
        rbTodos.setSelected(true);
        percentualDisponivel = null;
        percentualOcupado = null;
        ruaGlobal = null;
        paleteGlobal = null;
    }

}
